<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>公交站点查询</title>
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
  <style type="text/css">
    html,body,#container{
      height:100%;
    }
    .BMap_cpyCtrl
    {
      display: none;
    }
    .anchorBL {
      display: none;
    }
    .amap-logo img{
      display: none;
    }
    .amap-copyright {
      opacity:0;
    }
  </style>
</head>
<body>
<div id="container"></div>
<div id="tip" class='info' style='min-width:18rem;'></div>

<div class="input-card" style='width:18rem;'>
  <label style='color:grey'>公交站点查询</label>
  <div class="input-item">
    <div class="input-item-prepend"><span class="input-item-text" >站名</span></div>
    <input id='stationKeyWord' type="text">
  </div>
  <input id="search" type="button" class="btn" value="查询" />
</div>


<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=53b4e246765e8898dc8ecfa7f7a543e5&plugin=AMap.StationSearch"></script>
<script type="text/javascript">
  var map = new AMap.Map("container", {
    resizeEnable: true,
    center: [113.53591,34.817077],//地图中心点
    zoom: 14 //地图显示的缩放级别
  });
  var markers = [];
  stationSearch();
  /*公交站点查询*/
  function stationSearch() {
    // 移除原有marker
    map.remove(markers);
    markers = [];
    var stationKeyWord  = document.getElementById('stationKeyWord').value
    if(!stationKeyWord) return
    //实例化公交站点查询类
    var station = new AMap.StationSearch({
      pageIndex: 1, //页码
      pageSize: 60, //单页显示结果条数
      city: '010'   //确定搜索城市
    });
    station.search(stationKeyWord, function(status, result) {
      if (status === 'complete' && result.info === 'OK') {
        stationSearch_CallBack(result);
      } else {
        document.getElementById('tip').innerHTML = JSON.stringify(result);
      }
    });
  }
  /*公交站点查询返回数据解析*/
  function stationSearch_CallBack(searchResult) {
    var stationArr = searchResult.stationInfo;
    var searchNum = stationArr.length;
    if (searchNum > 0) {
      document.getElementById('tip').innerHTML = '查询结果：共' + searchNum + '个相关站点';
      for (var i = 0; i < searchNum; i++) {
        var marker = new AMap.Marker({
          icon:new AMap.Icon({
            image:'//a.amap.com/jsapi_demos/static/resource/img/pin.png',
            size:new AMap.Size(32,32),
            imageSize:new AMap.Size(32,32)
          }),
          offset:new AMap.Pixel(-16,-32),
          position: stationArr[i].location,
          map: map,
          title: stationArr[i].name
        });
        marker.info = new AMap.InfoWindow({
          content: stationArr[i].name,
          offset: new AMap.Pixel(0, -30)
        });
        marker.on('mouseover', function(e) {
          e.target.info.open(map, e.target.getPosition())
        })
        markers.push(marker);
      }
      map.setFitView();
    }
  }
  document.getElementById('search').onclick = stationSearch;
</script>
</body>
</html>